package com.ep.stepview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.ep.stepview.R;

/**
 * Created by Administrator on 2017/7/10.
 */

public class StepView extends View {
    //自定义属性:字体颜色、大小、外圆弧颜色、内圆弧颜色、圆弧宽度
    private int mTextColor=Color.RED;
    private int mTextSize;

    private int mOutColor= Color.BLUE;
    private int mInColor=Color.RED;
    private int mBorderWidth;

    //三个画笔
    private Paint mOutPaint;
    private Paint mInPaint;
    private Paint mTextPaint;


    private int mCurrentStep;//当前步数
    private int mMaxStep;//最大步数

    public StepView(Context context) {
        this(context,null);
    }

    public StepView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public StepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.StepView);
        mTextColor=typedArray.getColor(R.styleable.StepView_stepTextColor,mTextColor);
        mTextSize=typedArray.getDimensionPixelSize(R.styleable.StepView_stepTextSize,sp2px(mTextSize));

        mInColor=typedArray.getColor(R.styleable.StepView_inColor,mInColor);
        mOutColor=typedArray.getColor(R.styleable.StepView_outColor,mOutColor);
        mBorderWidth= (int) typedArray.getDimension(R.styleable.StepView_borderWidth,dp2px(mBorderWidth));

        typedArray.recycle();

        //初始化画笔
        mOutPaint=new Paint();
        mOutPaint.setAntiAlias(true);
        mOutPaint.setColor(mOutColor);
        mOutPaint.setStrokeCap(Paint.Cap.ROUND);
        mOutPaint.setStrokeWidth(mBorderWidth);
        mOutPaint.setStyle(Paint.Style.STROKE);

        mInPaint=new Paint();
        mInPaint.setAntiAlias(true);
        mInPaint.setColor(mInColor);
        mInPaint.setStrokeCap(Paint.Cap.ROUND);
        mInPaint.setStrokeWidth(mBorderWidth);
        mInPaint.setStyle(Paint.Style.STROKE);

        mTextPaint=new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width=MeasureSpec.getSize(widthMeasureSpec);
        int height=MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width>height?height:width,height>=width?width:height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画外圆弧 center=getwidth/2, radius=getWidth()/2-mBorderWidth/2
        RectF oval=new RectF(mBorderWidth/2,mBorderWidth/2,getWidth()-mBorderWidth/2,getHeight()-mBorderWidth/2);
        canvas.drawArc(oval,135,270,false,mOutPaint);

        if (mMaxStep==0)return;
        //内圆弧
        float sweepAngle=(float) mCurrentStep/mMaxStep;
        canvas.drawArc(oval,135,sweepAngle*270,false,mInPaint);
        //写文字
        String  text=mCurrentStep+"";
        Rect textBounds=new Rect();
        mTextPaint.getTextBounds(text,0,text.length(),textBounds);
        int dx=getWidth()/2-textBounds.width()/2;
        Paint.FontMetricsInt fontMetrics=mTextPaint.getFontMetricsInt();
        int dy=(fontMetrics.bottom-fontMetrics.top)/2-fontMetrics.bottom;
        int baseLine=getHeight()/2+dy;
        canvas.drawText(text,dx,baseLine,mTextPaint);
    }

    public void setMaxStep(int maxStep){
        this.mMaxStep=maxStep;
    }

    public void setCurStep(int curStep){
        this.mCurrentStep=curStep;
        invalidate();
    }


    private float dp2px(int borderWidth) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,borderWidth,getResources().getDisplayMetrics());
    }

    private int sp2px(int textSize) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,textSize,getResources().getDisplayMetrics());
    }
}
